package com.zhht.htzx.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zhht.htzx.R;
import com.zhht.htzx.model.OrderItem;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单商品项适配器
 */
public class OrderItemAdapter extends RecyclerView.Adapter<OrderItemAdapter.OrderItemViewHolder> {
    
    private Context context;
    private List<OrderItem> orderItems;
    
    public OrderItemAdapter(Context context) {
        this.context = context;
        this.orderItems = new ArrayList<>();
    }
    
    public void setOrderItems(List<OrderItem> orderItems) {
        this.orderItems = orderItems != null ? orderItems : new ArrayList<>();
        notifyDataSetChanged();
    }
    
    @NonNull
    @Override
    public OrderItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_order_product, parent, false);
        return new OrderItemViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull OrderItemViewHolder holder, int position) {
        OrderItem orderItem = orderItems.get(position);
        holder.bind(orderItem);
    }
    
    @Override
    public int getItemCount() {
        return orderItems.size();
    }
    
    static class OrderItemViewHolder extends RecyclerView.ViewHolder {
        
        private ImageView ivProduct;
        private TextView tvProductName;
        private TextView tvProductSpec;
        private TextView tvProductPrice;
        private TextView tvProductQuantity;

        public OrderItemViewHolder(@NonNull View itemView) {
            super(itemView);
            
            ivProduct = itemView.findViewById(R.id.iv_product);
            tvProductName = itemView.findViewById(R.id.tv_product_name);
            tvProductSpec = itemView.findViewById(R.id.tv_product_spec);
            tvProductPrice = itemView.findViewById(R.id.tv_product_price);
            tvProductQuantity = itemView.findViewById(R.id.tv_product_quantity);
        }
        
        public void bind(OrderItem orderItem) {
            // 设置商品名称
            tvProductName.setText(orderItem.getProductName());
            
            // 设置商品规格
            tvProductSpec.setText(orderItem.getSpecificationDisplayText());
            
            // 设置商品单价
            tvProductPrice.setText(orderItem.getPriceDisplayText());
            
            // 设置商品数量
            tvProductQuantity.setText("x" + orderItem.getQuantity());
            
            // 加载商品图片
            if (orderItem.getProductImage() != null && !orderItem.getProductImage().isEmpty()) {
                Glide.with(itemView.getContext())
                        .load(orderItem.getProductImage())
                        .placeholder(R.drawable.ic_product_placeholder)
                        .error(R.drawable.ic_product_placeholder)
                        .into(ivProduct);
            } else {
                ivProduct.setImageResource(R.drawable.ic_product_placeholder);
            }
        }
    }
} 